package com.diyizu.service.impl;

import com.diyizu.dao.IUserDao;
import com.diyizu.dao.impl.IUserDaoImpl;
import com.diyizu.dao.impl.ReadLogDao;
import com.diyizu.domain.User;
import com.diyizu.factory.Factory;
import com.diyizu.service.ILoginService;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class ILoginServiceImpl implements ILoginService {
    private static IUserDao iUserDao = Factory.getIUserDaoImpl();
    ReadLogDao readLogDao = Factory.getReadLogDao();

    @Override
    //判断用户名是否存在
    public boolean judgeUser(String username)  {
        ArrayList<User> list = iUserDao.getUser();
        for (User user : list) {
            if (user.getUsername().equals(username)) {
//                String password = user.getPassword();
//                iUserDao.updatePass(username,password);
                return false;
            }
        }
        return true;
    }

    @Override
    //判断原密码是否正确
    public boolean judgePass(String username,String password) {
        ArrayList<User> list = iUserDao.getUser();
        for (User user : list) {
            if (user.getUsername().equals(username)) {
                if (user.getPassword().equals(password)) {
//                    iUserDao.updatePass(username,password);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean ifPassWord(String username,String password){
        return iUserDao.isUser(username,password);
    }
    @Override
    //判断登录失败多次
    public boolean isLoginNum(String userName) {
        ArrayList<String> log = readLogDao.getLog();
        int loginNum = 0;
        for (int i = log.size() - 1; i >= 0; i--) {
            if (log.get(i).endsWith(userName + "用户登录失败")){
                loginNum++;
                if (loginNum==3){
                    return false;
                }
            }
            if (log.get(i).endsWith(userName + "用户登录成功")){
                break;
            }
        }
        return true;
    }
}
